#! /usr/local/bin/python

# Web 2.0
# A simple CGI script to retrieve a blog and return an HTML page
# 
# Note: fill in your own username and passwd!

import cgi
import urllib
import urllib2

username = '51158405'
passwd   = 'gxlty725'


















# get form variables (if desired)
form = cgi.FieldStorage()
# an example below
#n = str(form["n"].value)

# your username and password for the blog service
blogurl  = 'http://www.cs.cityu.edu.hk/~cs4380/webservice/blog/getPost.py'

# setup the password
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, 'http://www.cs.cityu.edu.hk/', username, passwd)
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)

# setup the parameters for calling the web service
params = urllib.urlencode({
	'author': username, 
	'type': 'html'
})

# call the webservice
data = urllib2.urlopen(blogurl, params).read()

# print out the content header
print 'Content-type: text/html\n\n'

# print out the xml/doctype header
print '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
'''

# print out the HTML header
print '''<head>
  <title>My Web 2.0 Blog</title>
  <link rel="stylesheet" type="text/css" media="all" href="sample.css"/>
</head>
'''

# print out HTML
print '<body><h1>My Blog</h1>'

# print the data (HTML formatted that we got from the webservice)
print data

# more HTML...
print '<hr><a href=index.html>back to form</a>\n'
print '</body>\n</html>'


